//
//  IndexRightView.m
//  WRY
//
//  Created by zhong sckj on 10-10-26.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "IndexRightView.h"
#import "SCCharts.h"
#import "NSArray+NSArrayExtra.h"

@implementation IndexRightView

@synthesize listData;


#pragma mark 画扇形
-(UIView *)drawsector
{
    NSMutableArray *dataArray = [[NSMutableArray alloc] init];
    if ([listData hasCount]) {
        //正常数值
        NSString *zhengchangValue=[(NSDictionary *)[listData objectAtIndex:0] objectForKey:@"NormalCount"];
        //超标
        NSString *chaobiaoValue=[(NSDictionary *)[listData objectAtIndex:0] objectForKey:@"SuperscaleCount"];
        //故障停产
        NSString *guzhangValue=[(NSDictionary *)[listData objectAtIndex:0] objectForKey:@"TroubleCount"];
        
        
        SC_PieChartData *data = [[SC_PieChartData alloc] init];
        data.value = [zhengchangValue intValue];
        data.title = @"正常";
        data.color = [UIColor greenColor];
        [dataArray addObject:data];
        data = [[SC_PieChartData alloc] init];
        data.value = [chaobiaoValue intValue];
        data.title = @"超标";
        data.color = [UIColor redColor];
        [dataArray addObject:data];
        data = [[SC_PieChartData alloc] init];
        data.value = [guzhangValue intValue];
        data.title = @"离线";
        data.color = [UIColor blueColor];
        [dataArray addObject:data];
        [data release];
    }
    
    SC_PieChartVC *cv = [[SC_PieChartVC alloc] init];
	[cv setViewFrame:CGRectMake(0.0, 0.0, 180.f, 120.0f)];
    [cv setData:dataArray];
	UIView *v=cv.view;
	[dataArray release];
	[cv release];
	return v;
}
#pragma mark 画柱状图
-(UIView *)drawHistogramView:(NSString *)detailtypecode
{
	
	NSString *total;
	//超标
	NSString *chaobiaoValue;
	//故障停产
	NSString *guzhangValue;
	//正常数值
	NSString *zhengchangValue;

	
	NSDictionary *directFeiShui=nil;
	NSDictionary *directFeiqi=nil;
	NSDictionary *directWushui=nil;
	NSDictionary *directTotal=nil;
	for (int i=0; i<[listData count]; i++) {
		NSString *enterpriseType=[(NSDictionary *)[listData objectAtIndex:i] objectForKey:@"DetailTypeCode"];
		NSDictionary *direct=(NSDictionary *)[listData objectAtIndex:i];
		if ([enterpriseType isEqualToString:@"01"]) {
			//废水
			directFeiShui=direct;
		}
		else if([enterpriseType isEqualToString:@"02"])
		{
			directFeiqi=direct;
		}
		else if([enterpriseType isEqualToString:@"03"])
		{
			directWushui=direct;
		}
		else {
			directTotal=direct;
		}
		
	}
	

	if ([listData count]>3) 
	{
		//废水
		if ([detailtypecode isEqualToString:@"01"])
		{
			
			total=[directFeiShui objectForKey:@"EnterpriseCount"];
			chaobiaoValue=[directFeiShui objectForKey:@"SuperscaleCount"];
			guzhangValue=[directFeiShui objectForKey:@"TroubleCount"];
			zhengchangValue=[directFeiShui objectForKey:@"NormalCount"];
			
		}
		else if([detailtypecode isEqualToString:@"02"])
		{
			//废气
			total=[directFeiqi objectForKey:@"EnterpriseCount"];
			chaobiaoValue=[directFeiqi objectForKey:@"SuperscaleCount"];
			guzhangValue=[directFeiqi objectForKey:@"TroubleCount"];
			zhengchangValue=[directFeiqi objectForKey:@"NormalCount"];
			
		}
		else {
			//污水处理厂
			total=[directWushui objectForKey:@"EnterpriseCount"];
			chaobiaoValue=[directWushui objectForKey:@"SuperscaleCount"];
			guzhangValue=[directWushui objectForKey:@"TroubleCount"];
			zhengchangValue=[directWushui objectForKey:@"NormalCount"];
			
		}
		
		
	}
		
	
	NSMutableArray *array = [[NSMutableArray alloc] init];
	
	for(int i = 0; i < 3; i++) 
	{
		SC_BarChartData *data = [[SC_BarChartData alloc] init];
		if (i == 0) {
			data.data =[NSArray arrayWithObject:chaobiaoValue];
			data.titles=[NSArray arrayWithObjects:@"超标",@"离线",@"正常",nil];
		}
		else if (i == 1) {
			data.data = [NSArray arrayWithObject:guzhangValue];
			
		}
		else {
			data.data =[NSArray arrayWithObject:zhengchangValue];
			
		}
		[array addObject:data];
		[data release];
	}
	
	
	
	
	SC_BarChartVC *barChartController=[[SC_BarChartVC alloc] init];
	[barChartController setFrame:CGRectMake(0.0f, 0.0f, 180, 120.f)];
	[barChartController setData:array];
	[barChartController setTitle:@"污染源统计"];
	UIView *v=barChartController.view;
	[array release];
	[barChartController release];
	return v;
	
}

-(id)init:(NSMutableArray *)dataArray
{
	if((self=[super init]))
	{
		self.listData=dataArray;
	}
	return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)dealloc {
	[listData release];
    [super dealloc];
}


@end
